/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.controller;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.orderscanner.UseOrdScaParams;
import com.lightkits.wms.domain.saleticket.CreateSaleTicketService;
import com.lightkits.wms.domain.saleticket.CreateUpSaleTicketService;
import com.lightkits.wms.domain.saleticket.QuerySaleMesService;
import com.lightkits.wms.dto.orderscanner.OrderScannerRequestType;
import com.lightkits.wms.dto.orderscanner.OrderScannerResponseType;
import com.lightkits.wms.dto.saleticket.GetComSaleRequestType;
import com.lightkits.wms.dto.saleticket.GetComSaleResponseType;
import com.lightkits.wms.dto.saleticket.GetSimSaleResponseType;
import com.lightkits.wms.dto.saleticket.QueryTagRequestType;
import com.lightkits.wms.dto.saleticket.QueryTagResponseType;
import com.lightkits.wms.dto.saleticket.SaleTicketRequestType;
import com.lightkits.wms.dto.saleticket.SaleTicketResponseType;
import com.lightkits.wms.dto.saleticket.UpSaleTicketRequestType;
import com.lightkits.wms.dto.saleticket.UpSaleTicketResponseType;
import com.lightkits.wms.dto.saleticket.model.SimSaleTicketDto;
import com.lightkits.wms.dto.usuallyinterface.GetSimTicketRequestType;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jing.wei@hand-china.com
 * @date 2019/5/31 5:40
 **/
@RestController
@RequestMapping("/SaleTicketService")
public class SaleTicketController {
    private final CreateSaleTicketService createSaleTicketService;

    @Autowired
    public SaleTicketController(CreateSaleTicketService createSaleTicketService,
                                UseOrdScaParams useOrdScaParams,
                                QuerySaleMesService querySaleMesService,
                                CreateUpSaleTicketService createUpSaleTicketService) {
        this.createSaleTicketService = createSaleTicketService;
        this.useOrdScaParams = useOrdScaParams;
        this.querySaleMesService = querySaleMesService;
        this.createUpSaleTicketService = createUpSaleTicketService;
    }

    /**
     * @param request SaleTicketRequestType
     * @return SaleTicketResponseType
     */
    @PostMapping(value = "/createSaleTicket")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/createSaleTicket")
    @ResponseBody
    //售货单的创建
    public SaleTicketResponseType createSaleTicket(@RequestBody(required = false) SaleTicketRequestType request) {
        SaleTicketResponseType response = new SaleTicketResponseType();
        response.setSuccessful(true);
        try {
            IResult checkResult = createSaleTicketService.createSaleTicket(request.getHeadDto(),
                    request.getLineDtos());
            if (!checkResult.isSuccessful()) {
                return checkResult.fill(response);
            }
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("Create Sale Ticket Faild");
        }
        return response;
    }

    private final UseOrdScaParams useOrdScaParams;

    /**
     * @param request OrderScannerRequestType
     * @return OrderScannerResponseType
     */
    @PostMapping(value = "/scannerOrder")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/scannerOrder")
    @ResponseBody
    //售货单的创建
    public OrderScannerResponseType scannerOrder(@RequestBody(required = false) OrderScannerRequestType request) {
        OrderScannerResponseType response = new OrderScannerResponseType();
        response.setSuccessful(true);
        try {
            IResult checkResult = useOrdScaParams.scannerValue(request.getHeaderDto(),
                    request.getLineDtos());
            if (!checkResult.isSuccessful()) {
                return checkResult.fill(response);
            }
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("Create Order Scanner Faild");
        }
        return response;
    }

    private final QuerySaleMesService querySaleMesService;

    /**
     * @param request GetSimTicketRequestType
     * @return GetSimSaleResponseType
     */
    @PostMapping(value = "/getSImSale")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getSImSale")
    @ResponseBody
    //售货单的创建
    public GetSimSaleResponseType getSImSale(
            @RequestBody @Validated GetSimTicketRequestType request) {
        GetSimSaleResponseType response = new GetSimSaleResponseType();
        response.setSuccessful(true);
        List<SimSaleTicketDto> simSaleTicket = querySaleMesService.getSimSaleTicket(
                request.getTicketNumOrPartyName(), request.getOrgId());
        if (!ObjectUtil.isNullOrEmpty(simSaleTicket)) {
            response.setPageDataCount(simSaleTicket.size());
        }
        response.setSimSaleTicketDtos(simSaleTicket);
        return response;
    }

    /**
     * @param request GetComSaleRequestType
     * @return GetComSaleResponseType
     */
    @PostMapping(value = "/getComSale")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getComSale")
    @ResponseBody
    //售货单的创建
    public GetComSaleResponseType getComSale(
            @RequestBody @Validated GetComSaleRequestType request) {
        GetComSaleResponseType response = new GetComSaleResponseType();
        response.setSuccessful(true);
        response.setComSaleTicketDtos(querySaleMesService.getComSaleTicket(
                request.getTicketId(), request.getOrgId(), request.getTagCode()));
        return response;
    }

    private final CreateUpSaleTicketService createUpSaleTicketService;

    /**
     * @param request UpSaleTicketRequestType
     * @return UpSaleTicketResponseType
     */
    @PostMapping(value = "/upSaleTicket")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/upSaleTicket")
    @ResponseBody
    public UpSaleTicketResponseType upSaleTicket(@RequestBody @Validated UpSaleTicketRequestType request) {
        UpSaleTicketResponseType response = new UpSaleTicketResponseType();
        createUpSaleTicketService.createSaleTicket(request.getHeadDto(), request.getLineDtos()).fill(response);
        return response;
    }

    /**
     * @param request QueryTagRequestType
     * @return QueryTagResponseType
     */
    @PostMapping(value = "/queryTagMes")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/queryTagMes")
    @ResponseBody
    public QueryTagResponseType queryTagMes(
            @RequestBody @Validated QueryTagRequestType request) {
        QueryTagResponseType response = new QueryTagResponseType();
        response.setSuccessful(true);
        response.setTicketDto(querySaleMesService.getComSaleTicket(
                request.getBookStatus(), request.getOrgId(), request.getTagCode()));
        return response;
    }

}
